<template>
  <div class="container">
    <el-form
      style="background-color: white"
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <img src="static/img/kd.png" alt="Description" />
      <el-form-item label="用户名" prop="userName">
        <el-input v-model.number="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from "@/api/api.js"

export default {
  data() {
    return {
      ruleForm: {
        pass: "",
        userName: ""
      },
      rules: {
        pass: [{ required: true, message: "请输入密码", trigger: "blur" }],
        userName: [{ required: true, message: "请输入用户名", trigger: "blur" }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          api
            .post("/sys/login", this.ruleForm)
            .then(response => {
              // 处理成功的响应
              var userId = response.data.loginMap.userId
              var token = response.data.loginMap.token
              var result = response.data.result
              if (result === "success") {
                localStorage.setItem("token", token)
                localStorage.setItem("userId", userId)
                this.$nextTick(() => {
                  this.$router.push("/home")
                })
              } else {
                var message = response.data.message
                alert(message)
              }
            })
            .catch(error => {
              // alert("用户名或密码不正确!");
            })
        } else {
          console.log("error submit!!")
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less">
.container {
  display: flex;
  justify-content: center;
  /* 水平居中 */
  align-items: center;
  /* 垂直居中 */
  height: 100vh;
  /* 示例：设置容器高度为视口高度 */
}

.el-form-item__label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: black;
  line-height: 40px;
  padding: 0 12px 0 0;
  box-sizing: border-box;
}

.demo-ruleForm {
  width: 500px;
  /* 设置宽度 */
  max-width: 100%;
  /* 确保在响应式布局下也能良好工作 */
  padding: 50px;
}
</style>
